package com.tony.xml;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

/**
 * @version V1.0  </br>
 * @ClassName XMLDemo </br>
 * @Description TODO  </br>
 * @Author tonywang</br>
 * @DATE 3/7/2019 5:29 PM</br>
 */
public class XMLDemo extends DefaultHandler{

    public static void main(String[] args){
        XMLDemo.domDemo();
    }

    public static void domDemo() {
        long last = System.currentTimeMillis();
        try {
            File file = new File("C:\\Users\\tonywang\\Desktop\\springboot\\springBoot\\src\\main\\resources\\test.xml");
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
            Document document = builder.parse(file);
            NodeList nodeList = document.getElementsByTagName("value");
            for (int i=0; i < nodeList.getLength(); i++) {
                System.out.println("车牌号码:" + document.getElementsByTagName("No").item(i).getFirstChild().getNodeValue());
                System.out.println("车牌号码:" + document.getElementsByTagName("Addr").item(i).getFirstChild().getNodeValue());
            }
        } catch (Exception pE) {
            pE.printStackTrace();
        }
        System.out.println("运行时间：" + (System.currentTimeMillis() - last) + "毫秒");
    }

    //sax
    java.util.Stack tags = new java.util.Stack();
    public static void saxDemo() {
        long lasting = System.currentTimeMillis();
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = saxParserFactory.newSAXParser();
            XMLDemo xmlDemo = new XMLDemo();
            saxParser.parse(new InputSource("C:\\Users\\tonywang\\Desktop\\springboot\\springBoot\\src\\main\\resources\\test.xml"), xmlDemo);
        } catch (ParserConfigurationException pE) {
            pE.printStackTrace();
        } catch (SAXException pE) {
            pE.printStackTrace();
        } catch (IOException pE) {
            pE.printStackTrace();
        }
        System.out.println("运行时间：" + (System.currentTimeMillis() - lasting) + "毫秒");
    }
    //sax
    public void characters(char ch[], int start, int length) throws SAXException {
        String tag = (String) tags.peek();
        if (tag.equals("No")) {
        System.out.print("车牌号码：" + new String(ch, start, length));
        }
        if (tag.equals("Addr")) {
            System.out.println("地址:" + new String(ch, start, length));
        }
    }
    //sax
    public void startElement(String uri,String localName,String qName,Attributes attrs) {
        tags.push(qName);
    }

}
